package shiyan6;

public class BreadConsumer extends Thread {

    private static final long SLEEP_MILLIS = 120L;

    private final YiXinZhiShop shop;
    private final int consumptionCount;

    public BreadConsumer(String name, YiXinZhiShop shop, int consumptionCount) {
        super(name);
        this.shop = shop;
        this.consumptionCount = consumptionCount;
    }

    @Override
    public void run() {
        for (int i = 0; i < consumptionCount; i++) {
            Bread bread = shop.consumeBread();
            if (bread == null) {
                break;
            }
            try {
                Thread.sleep(SLEEP_MILLIS);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println(getName() + " 被中断: " + e.getMessage());
                break;
            }
        }
    }
}
